package org.springframework.boot;

import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;
import org.springframework.core.env.SimpleCommandLinePropertySource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.ResourcePropertySource;

import java.io.IOException;

/**
 * @author shiminghui
 * @date 2025/5/7 20:21
 * @description: TODO
 */
public class Step3 {
    public static void main(String[] args) throws IOException {
        ApplicationEnvironment applicationEnvironment = new ApplicationEnvironment(); // 系统环境变量  xml  yml  properties
        MutablePropertySources propertySources = applicationEnvironment.getPropertySources();
        propertySources.addLast(new ResourcePropertySource(new ClassPathResource("step3.properties"))); //解析配置文件,这里没有解析yml的实现类,还是测试properties文件吧
        propertySources.addFirst(new SimpleCommandLinePropertySource(args)); //解析命令行参数

        // 优先获取systemProperties(系统属性)里面的值,如果不存在则从systemEnvironment(系统环境变量)里面获取
        for (PropertySource<?> propertySource : propertySources) {
            System.out.println(propertySource.getName());
            Object source = propertySource.getSource();
            System.out.println("\t属性值: " + source);
        }
        // 虚拟机参数就是 -D 开头的参数,是 systemProperties 里面的值
        System.out.println(applicationEnvironment.getProperty("JAVA_HOME"));
        // 优先读取命令行参数
        System.out.println(applicationEnvironment.getProperty("server.port"));
    }
}
